package com.mx.recycleview.anytype

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.mx.recycleview.base.BaseSimpleAdapt
import com.mx.recycleview.base.BaseViewHolder
import java.util.concurrent.atomic.AtomicInteger

abstract class BaseTypeAdapt<T>(list: ArrayList<T>? = null) :
    BaseSimpleAdapt<T>(list ?: arrayListOf<T>()) {
    private val currentViewType by lazy { viewTypeIndex.incrementAndGet() }

    open fun getSpanSize(): Int = 1

    final override fun getItemViewType(position: Int): Int {
        return currentViewType
    }

    final override fun createItem(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): View {
        return createItem(inflater, parent)
    }

    abstract fun createItem(inflater: LayoutInflater, parent: ViewGroup): View
    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        super.onBindViewHolder(holder, position - itemIndexStart)
    }

    fun getAdaptViewType() = currentViewType

    fun setStartIndex(startIndex: Int) {
        itemIndexStart = startIndex
    }

    private var itemIndexStart: Int = 0

    companion object {
        private val viewTypeIndex = AtomicInteger()
    }
}